// 基础方法
async function baseGet(url, params, token) {
    if (params) {
        let paramsArray = [];
        //拼接参数  
        Object.keys(params).forEach(key => paramsArray.push(key + '=' + params[key]))
        if (url.search(/\?/) === -1) {
            url += '?' + paramsArray.join('&');
        } else {
            url += '&' + paramsArray.join('&');
        }
    }

    // console.log("aaaaaaaaaaaaaaaa");
    // console.log(token);
    //fetch请求  
    return await fetch(url, {
        method: 'GET',
        headers: {
            "Accept": "application/json",
            'Content-Type': 'application/json',
            'Authorization': "Bearer " + token
        }
    }).then(response => {
        if (response.ok) {
            return response.json();
        }
        return response;
    }).then(responseJson => {
        return responseJson;
    }).catch((error) => {
        return { code: 404, result: [] };
    })
}

async function basePost(url, data, token) {
    // console.log("####### basePost #######")
    // console.log(data, url)
    return await fetch(url, {
        method: 'POST',
        mode: 'cors',
        headers: {
            "Accept": "application/json",
            "Connection": "keep-alive",
            "Content-Type": "application/json",
            'Authorization': "Bearer " + token || {}//token
        },

        body: JSON.stringify(data || {})
    }).then(response => {
        // console.log("####### basePost response  #######111")
        // console.log(response)
        if (response.ok) {
            return response.json();
        }
        return response;
    }).then(responseJson => {
        // console.log("####### basePost responseJson  #######222")
        // console.log(responseJson)
        return responseJson;
    }).catch((error) => {
        return { code: 404, result: [] };
    })
}
async function baseDelete(url, data, token) {
    // console.log("####### basePost #######")
    // console.log(data, url)
    return await fetch(url, {
        method: 'DELETE',
        mode: 'cors',
        headers: {
            "Accept": "application/json",
            "Connection": "keep-alive",
            "Content-Type": "application/json",
            'Authorization': "Bearer " + token || {}//token
        },

        body: JSON.stringify(data || {})
    }).then(response => {
        // console.log("####### basePost response  #######111")
        // console.log(response)
        if (response.ok) {
            return response.json();
        }
        return response;
    }).then(responseJson => {
        // console.log("####### basePost responseJson  #######222")
        // console.log(responseJson)
        return responseJson;
    }).catch((error) => {
        return { code: 404, result: [] };
    })
}

async function basePostWithQuery(url, data, token) {
    if (data) {
        let paramsArray = [];
        //拼接参数  
        Object.keys(data).forEach(key => paramsArray.push(key + '=' + data[key]))
        if (url.search(/\?/) === -1) {
            url += '?' + paramsArray.join('&');
        } else {
            url += '&' + paramsArray.join('&');
        }
    }
    return await fetch(url, {
        method: 'POST',
        mode: 'cors',
        headers: {
            "Accept": "application/json",
            'Content-Type': 'application/json',
            "Connection": "close",
            'Authorization': "Bearer " + token
        },
    }).then(response => {
        if (response.ok) {
            return response.json();
        }
        return response;
    }).then(responseJson => {
        return responseJson;
    }).catch((error) => {
        return { code: 404, result: [] };
    })
}

module.exports = {
    baseGet: baseGet,
    basePost: basePost,
    basePostWithQuery: basePostWithQuery,
    baseDelete: baseDelete
}